﻿/*
    - Version: 1.1
    - Huydq: 2016-06-03: luôn hiện thị text comment đầy đủ (không dùng substring)
*/
var Dieuchinhgiuanam = function () {
    lstcomment = [];
    lstcommentsend = [];
    lstcommentreview = [];
    lstcommentkhtc = [];
    lstvalue1 = [];
    lstvalue2 = [];
    lstcommentstatus2 = [];
    lstcommentstatus3 = [];
    lstcommentstatus4 = [];
    checkxem = 0;
    checkdhqg = 0;
    function loaddatatotable(nhomchitieu, nhomdonvi, dt, initData, nkhid) {
        var lstchitieucap2;
        var lstdonvi;
        $.ajax({
            url: initData.Getallchitieubymacap2,
            type: "GET",
            headers: {
                'VerificationToken': initData.VerificationToken
            },
            dataType: "json",
            async: false,
            data: {
                'namchitieuid': nkhid,
                'macap2': nhomchitieu
            },
            success: function (data) {
                lstchitieucap2 = data;

            },
            error: function (jqXHR, textStatus, errorThrown) {
                window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
            },
            complete: function () {

            }
        });
        $.ajax({
            url: initData.GetAllDonViByNhomID,
            type: "GET",
            headers: {
                'VerificationToken': initData.VerificationToken
            },
            dataType: "json",
            async: false,
            data: {
                'nhomdvid': nhomdonvi
            },
            success: function (data) {
                lstdonvi = data;
                if (data.length > 8) {
                    $('#tbdata').css('width', '2000px');
                    $('.chonnhom').css('width', '16%');
                    $('.dvt').css('width', '4%');
                    $('.ct').css('width', '8%');
                    $('.cl').css('width', '66%');
                } else {
                    $('#tbdata').css('width', '100%');
                    $('.chonnhom').css('width', '23%');
                    $('.dvt').css('width', '7%');
                    $('.ct').css('width', '13.5%');
                    $('.cl').css('width', '56.5%');
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
            },
            complete: function () {

            }
        });
        $('.cl').attr('colspan', lstdonvi.length);
        $('#hdernew').empty();
        $('#hdernew').append('<th>Chỉ tiêu</th>\
                                <th></th>\
                                <th class="ct">Định hướng</th>\
                                <th class="ct">Cân đối</th>');
        $.each(lstdonvi, function (i, v) {
            $('#hdernew').append(' <th class="ct">' + v.TenVietTat + '</th>')
        });
        $('#bdy').empty();
        $.each(lstchitieucap2, function (i, v) {
            var data = '<tr>\
                                <td>' + v.TEN_CHI_TIEU_CAP3 + '</td>\
                                <td>' + v.DON_VI_TINH + '</td>\
                                <td><input type="text"  disabled="disabled" class="e-input3 form-control" level="1" chitieuid="'+ v.CHI_TIEU_ID + '" value="" /></td>\
                                <td><input type="text" disabled="disabled" level="2" class="e-input1 form-control cd'+ i + '" value="" /></td>';
            $.each(lstdonvi, function (x, y) {
                if (x % 2 == 0) {
                    data += '<td><input type="text" disabled="disabled" class="e-input2 form-control " level="3" name="cd' + i + '" chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + y.DonViId + '" value="" /></td>';
                } else {
                    data += '<td><input type="text" disabled="disabled" class="e-input3 form-control " level="3" name="cd' + i + '" chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + y.DonViId + '" value="" /></td>';
                }
            });

            data += '</tr>\
                            <tr>\
                                <td><span class="glyphicon glyphicon-record" style="margin-left:10%"></span><label style="margin-left:3%;">Kế hoạch đầu năm</label></td>\
                                <td></td>\
                                <td></td>\
                            <td><input type="text" disabled="disabled" class="e-input1 form-control " value="" /></td>';
            $.each(lstdonvi, function (x, y) {
                data += '<td><input type="text" disabled="disabled" class="e-input1 form-control" chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + y.DonViId + '" level="5"  name="cd1" value="" /></td>';
            });
            data += '</tr>';
            $('#bdy').append(data);
        });

        $.ajax({
            url: initData.GetAllDvCtbyNKH,
            type: "GET",
            headers: {
                'VerificationToken': initData.VerificationToken
            },
            dataType: "json",
            async: false,
            data: {
                'namkhid': nkhid,
                'flag': 0
            },
            success: function (data) {
                $.each(data, function (x, y) {
                    $('[chitieuid="' + y.CHI_TIEU_ID + '"][donviid="' + y.DON_VI_ID + '"][level="3"]').removeAttr('disabled');
                });
                $.ajax({
                    url: initData.GetKehoachbyNamkehoach,
                    type: "GET",
                    headers: {
                        'VerificationToken': initData.VerificationToken
                    },
                    dataType: "json",
                    async: false,
                    data: {
                        'namkhid': nkhid,
                        'statusid': dt,
                        'flag': 0
                    },
                    success: function (data) {
                        $.each(data, function (x, y) {
                            $('[chitieuid="' + y.CHI_TIEU_ID + '"][donviid="' + y.DON_VI_ID + '"][level="3"]').val(y.GIA_TRI);
                        });
                        $.ajax({
                            url: initData.GetKehoachbyNamkehoach,
                            type: "GET",
                            headers: {
                                'VerificationToken': initData.VerificationToken
                            },
                            dataType: "json",
                            async: false,
                            data: {
                                'namkhid': nkhid,
                                'statusid': 0,
                                'flag': 0
                            },
                            success: function (data) {
                                $.each(data, function (i, v) {
                                    $('[chitieuid="' + v.CHI_TIEU_ID + '"][level="1"]').val(v.GIA_TRI);
                                });
                                $.ajax({
                                    url: initData.GetKehoachbyNamkehoach,
                                    type: "GET",
                                    headers: {
                                        'VerificationToken': initData.VerificationToken
                                    },
                                    dataType: "json",
                                    async: false,
                                    data: {
                                        'namkhid': nkhid,
                                        'statusid': 51,
                                        'flag': 0
                                    },
                                    success: function (data) {
                                        $.each(data, function (x, y) {
                                            if ($('[chitieuid="' + y.CHI_TIEU_ID + '"][donviid="' + y.DON_VI_ID + '"][level="3"]').val() == "") {
                                                $('[chitieuid="' + y.CHI_TIEU_ID + '"][donviid="' + y.DON_VI_ID + '"][level="3"]').val(y.GIA_TRI);
                                                $('[chitieuid="' + y.CHI_TIEU_ID + '"][donviid="' + y.DON_VI_ID + '"][level="3"]').css("color", "red")
                                            }

                                        });
                                        $.ajax({
                                            url: initData.GetKehoachbyNamkehoach,
                                            type: "GET",
                                            headers: {
                                                'VerificationToken': initData.VerificationToken
                                            },
                                            dataType: "json",
                                            async: false,
                                            data: {
                                                'namkhid': nkhid,
                                                'statusid': 31,
                                                'flag': 0
                                            },
                                            success: function (data) {
                                                $.each(data, function (x, y) {
                                                    $('[chitieuid="' + y.CHI_TIEU_ID + '"][donviid="' + y.DON_VI_ID + '"][level="5"]').val(y.GIA_TRI);
                                                });
                                                $('.restartall').blur(function () {
                                                    if ($(this).val() != "") {
                                                        if (!isNumeric($(this).val())) {
                                                            alert('Bạn vui lòng nhập kiểu số !');
                                                            $(this).val('');
                                                        }
                                                    }
                                                });
                                                $(".restartall").each(function () {
                                                    var b = $(this).attr('name');
                                                    calculateSum(b);
                                                    $(this).keyup(function () {
                                                        var a = $(this).attr('name');
                                                        calculateSum(a);
                                                    });
                                                });
                                            },
                                            error: function (jqXHR, textStatus, errorThrown) {
                                                window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                                            },
                                            complete: function () {
                                            }
                                        });
                                    },
                                    error: function (jqXHR, textStatus, errorThrown) {
                                        window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                                    },
                                    complete: function () {
                                    }
                                });
                            },
                            error: function (jqXHR, textStatus, errorThrown) {
                                window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                            },
                            complete: function () {
                            }
                        });
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                    },
                    complete: function () {

                    }
                });
            },
            error: function (jqXHR, textStatus, errorThrown) {
                window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
            },
            complete: function () {

            }
        });


    }
    function arrayDiff(a1, a2) {
        var o1 = {}, o2 = {}, diff = [], i, len, k;
        for (i = 0, len = a1.length; i < len; i++) { o1[a1[i]] = true; }
        for (i = 0, len = a2.length; i < len; i++) { o2[a2[i]] = true; }
        for (k in o1) { if (!(k in o2)) { diff.push(k); } }
        for (k in o2) { if (!(k in o1)) { diff.push(k); } }
        return diff;
    }
    function checkexit(lstcommentedited, chitieu, donvi) {
        var kq = false;
        $.each(lstcommentedited, function (i) {
            if (parseInt(lstcommentedited[i].CHI_TIEU_ID) === parseInt(chitieu) && parseInt(lstcommentedited[i].DON_VI_ID) == parseInt(donvi)) {
                kq = true;
                return false;
            }
        });
        return kq;
    }
    function loadtable(namkh, initData) {
        if (initData.DonviID != 0) {
            $('#Xemfull').show();
            $.ajax({
                url: initData.GetallCommentByDv,
                type: "GET",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                data: {
                    'namkh': namkh,
                    'moc': 2,
                    'flag': 2
                },
                async: false,
                success: function (data) {
                    $('#lstdstruong').empty();
                    $('#body').empty();
                    if (data.length > 0 && data != "err") {
                        $('#thongbao').text('');
                        $('#Create').prop('disabled', false);
                        $('#gridContent').show();
                        lstcomment = [];
                        var mtpdv = 0;
                        $.each(data, function (i, v) {
                            var comment = v.COMMENT_DV;
                            //if (comment.length > 40) {
                            //    comment = comment.substring(0, 40) + "...";
                            //}
                            if (v.DON_VI_ID != mtpdv) {
                                $('#lstdstruong').append('<option value="' + v.DON_VI_ID + '">' + v.TenDV + '</option>');
                                mtpdv = v.DON_VI_ID;
                            }
                            var row = '<tr class="webgrid-row-style">\
                                        <td class="clsSTT" style="text-align: center; width: 30px;">\
                                            '+ (i + 1) + '\
                                        </td>\
                                        <td>\
                                            <div id="spnFullName" class="display-mode">\
                                                <span class="hidden-780 tenbc">' + $('#Namlapbc option:selected').text() + '</span>\
                                            </div>\
                                        </td>\
                                        <td class="clsUserName hidden-480">\
                                            <span id="spnUserName" class="display-mode timeline">' + v.TEN_CHI_TIEU + '</span>\
                                        </td>\
                                        <td class="clsUserName hidden-480">\
                                            <span id="spnUserName" class="display-mode timeline">\
                                                ' + v.TenDV + '\
                                            </span>\
                                        </td>\
                                        <td class="clsUserName hidden-480">\
                                            <span id="spnUserName" class="display-mode timeline">\
                                                    <label title="' + v.COMMENT_DV + '">\
                                                        ' + comment + '\
                                                </label>\
                                            </span>\
                                        </td>\
                                    </tr>';
                            $('#body').append(row);
                            lstcomment.push(v);
                        });
                        $('#bd').empty();
                        var dv = $('#lstdstruong').val();
                        var checkcap2 = "";
                        $.each(lstcomment, function (i, v) {
                            if (v.DON_VI_ID == dv) {
                                var dc = "";
                                if (v.DieuChinh != null) {
                                    dc = v.DieuChinh;
                                }
                                if (v.MA_CAP2 != checkcap2) {
                                    $('#bd').append('<tr>\
                                    <td style="font-weight: bold;">' + v.TEN_CHI_TIEU_CAP2.toUpperCase() + '</td>\
                                     <td></td>\
                                    <td></td>\
                                    <td></td>\
                                    <td></td>\
                                    <td></td>\
                                    <td></td>\
                                    <td class="thaotac"></td>\
                                </tr>');
                                    checkcap2 = v.MA_CAP2;
                                }
                                var show = v.COMMENT_DV;
                                //if (v.COMMENT_DV.length > 15) {
                                //    show = v.COMMENT_DV.substring(0, 12) + "...";
                                //}
                                $('#bd').append('<tr chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '">\
                                <td>' + v.TEN_CHI_TIEU + '</td>\
                                 <td>' + v.DON_VI_TINH + '</td>\
                                <td><input type="text" disabled="disabled"  chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" class="e-input1 form-control" level="1" value="' + v.KeHoachDauNam + '" /></td>\
                                <td><input type="text"   chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" class="e-input3 form-control" level="3" value="' + v.ThucHien + '" /></td>\
                                <td><input type="text"   chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" class="e-input3 form-control" level="61" value="' + v.ThucHien + '" /></td>\
                                <td><label title="Nội dung: ' + v.COMMENT_DV + '">' + show + '</label></td>\
                                <td><input type="button" class="btn btn-danger reject"  chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" style="width: 90px;" data-html="true" data-placement="bottom" level="12" title="Nội dung: ' + v.COMMENT_DV + '"  value="Ý kiến" /></td>\
                                <td><button type="button" chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '"  class="btn btn-primary approve">Duyệt & Gửi</button></td>\
                            </tr>');
                            }
                        });
                    } else {
                        $.ajax({
                            url: initData.GetallCommentByDv,
                            type: "GET",
                            headers: {
                                'VerificationToken': initData.VerificationToken
                            },
                            dataType: "json",
                            data: {
                                'namkh': namkh,
                                'moc': 2,
                                'flag': 3
                            },
                            async: false,
                            success: function (data) {
                                if (data.length == 0) {
                                    $('#gridContent').hide();
                                    $('#thongbao').text('Không có đơn vị nào gửi lên điều chỉnh !');
                                } else {
                                    $('#body').empty();
                                    $('#gridContent').show();
                                    $.each(data, function (i, v) {
                                        var comment = v.COMMENT_DV;
                                        //if (comment.length > 40) {
                                        //    comment = comment.substring(0, 40) + "...";
                                        //}
                                        var row = '<tr class="webgrid-row-style">\
                                        <td class="clsSTT" style="text-align: center; width: 30px;">\
                                            '+ (i + 1) + '\
                                        </td>\
                                        <td>\
                                            <div id="spnFullName" class="display-mode">\
                                                <span class="hidden-780 tenbc">' + $('#Namlapbc option:selected').text() + '</span>\
                                            </div>\
                                        </td>\
                                        <td class="clsUserName hidden-480">\
                                            <span id="spnUserName" class="display-mode timeline">' + v.TEN_CHI_TIEU + '</span>\
                                        </td>\
                                        <td class="clsUserName hidden-480">\
                                            <span id="spnUserName" class="display-mode timeline">\
                                                ' + v.TenDV + '\
                                            </span>\
                                        </td>\
                                        <td class="clsUserName hidden-480">\
                                            <span id="spnUserName" class="display-mode timeline">\
                                                    <label title="' + v.COMMENT_DV + '">\
                                                        ' + comment + '\
                                                </label>\
                                            </span>\
                                        </td>\
                                    </tr>';
                                        $('#body').append(row);
                                    });
                                    $('#thongbao').text("");
                                }
                            },
                            error: function (jqXHR, textStatus, errorThrown) {
                                window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                            },
                            complete: function () {
                            }
                        });
                        $('#Create').prop('disabled', true);

                    }

                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {
                }
            });

            $.ajax({
                url: initData.GetallCommentByDv,
                type: "GET",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                data: {
                    'namkh': namkh,
                    'moc': 2,
                    'flag': 8
                },
                async: false,
                success: function (data) {
                    $('#bodykhtc').empty();
                    if (data.length > 0) {
                        $.each(data, function (i, v) {
                            var row = '<tr class="webgrid-row-style">\
                                        <td class="clsSTT" style="text-align: center; width: 30px;">\
                                            '+ (i + 1) + '\
                                        </td>\
                                        <td>\
                                            <div id="spnFullName" class="display-mode">\
                                                <span class="hidden-780 tenbc">' + $('#Namlapbc option:selected').text() + '</span>\
                                            </div>\
                                        </td>\
                                        <td class="clsUserName hidden-480">\
                                            <span id="spnUserName" class="display-mode timeline">' + v.TEN_CHI_TIEU + '</span>\
                                        </td>\
                                        <td class="clsUserName hidden-480">\
                                            <span id="spnUserName" class="display-mode timeline">\
                                                ' + v.TenDV + '\
                                            </span>\
                                        </td>\
                                        <td class="clsUserName hidden-480">\
                                            <span id="spnUserName" class="display-mode timeline">\
                                                    <label title="' + v.COMMENT_DV + '">\
                                                        ' + v.COMMENT_DV + '\
                                                </label>\
                                            </span>\
                                        </td>\
                                    </tr>';
                            $('#bodykhtc').append(row);
                        });
                        $('#gridContentbankhtc').show();
                    } else {
                        $('#gridContentbankhtc').hide();
                    }
                    
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {
                }
            });


        } else {
            $('#Xemfull').hide();
        }

    }
    var initLoadData = function (initData) {
        $.ajax({
            url: initData.GetallNamkehoach,
            type: "GET",
            headers: {
                'VerificationToken': initData.VerificationToken
            },
            dataType: "json",
            async: false,
            success: function (data) {

                var lst = [];
                $('#Namlapbc').empty();
                $('#Namlapbc').append('<option value="-1">[--Chọn năm kế hoạch--]</option>');
                $.each(data, function (i, v) {
                    if (v.STATUS_ID >= 50) {
                        $('#Namlapbc').append('<option value="' + v.NAM_KE_HOACH_ID + '">' + v.NAM_KE_HOACH + '</option>');
                        lst.push(v);
                    }
                });
                var namkh = $.cookie('mkhidchung');
                if (namkh != undefined) {
                    $('#Namlapbc').val(namkh);
                    loadtable(namkh, initData);
                    $.session.set('nkhid', namkh);
                }
                data = lst;
            },
            error: function (jqXHR, textStatus, errorThrown) {
                window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
            },
            complete: function () {
            }
        });
        if (initData.DonviID == 0) {
            $('#gridContent').hide();
            $('#Create').hide();
            $('#Update').hide();            
            $('#Duyet').show();
        } else {
            $('#gridContent').show();
            $('#Update').show();
            $('#Create').show();
            $('#Duyet').hide();
        }

    };
    var initButtonClick = function (initData) {
        $('#Create').on('click', function () {
            checkxem = 0;
            $.ajax({
                url: initData.GetNamkehoachbyID,
                type: "GET",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                data: {
                    'id': $('#Namlapbc').val(),
                },
                async: false,
                success: function (data) {
                    lstcommentsend = [];
                    if (data.STATUS_ID < 70) {
                        $("#Tit").text("Duyệt đề xuất điều chỉnh giữa năm");
                        $('.dsdv').attr('colspan', 6);
                        $('.thaotac').show();
                        $('.cmkhtcbutton').hide();
                        $.ajax({
                            url: initData.GetallCommentByDv,
                            type: "GET",
                            headers: {
                                'VerificationToken': initData.VerificationToken
                            },
                            dataType: "json",
                            data: {
                                'namkh': $('#Namlapbc').val(),
                                'moc': 2,
                                'flag': 2
                            },
                            async: false,
                            success: function (data) {
                                lstcomment = [];
                                var mtpdv = 0;
                                $('#lstdstruong').empty();
                                $.each(data, function (i, v) {
                                    if (v.DON_VI_ID != mtpdv) {
                                        $('#lstdstruong').append('<option value="' + v.DON_VI_ID + '">' + v.TenDV + '</option>');
                                        mtpdv = v.DON_VI_ID;
                                    }

                                    lstcomment.push(v);
                                });
                            },
                            error: function (jqXHR, textStatus, errorThrown) {
                                window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                            },
                            complete: function () {
                            }
                        });
                        $("#ApproveAll").show();
                        $("#Approve").show();
                        $('#Huy').hide();
                        $('#bd').empty();
                        var dv = $('#lstdstruong').val();
                        lstvalue1 = [];
                        var checkcap2 = "";
                        $.each(lstcomment, function (i, v) {
                            if (v.DON_VI_ID == dv) {
                                var dc = "";
                                if (v.DieuChinh != null) {
                                    dc = v.DieuChinh;
                                }
                                if (v.MA_CAP2 != checkcap2) {
                                    $('#bd').append('<tr>\
                                    <td style="font-weight: bold;">' + v.TEN_CHI_TIEU_CAP2.toUpperCase() + '</td>\
                                     <td></td>\
                                    <td></td>\
                                    <td></td>\
                                    <td></td>\
                                    <td></td>\
                                    <td></td>\
                                    <td class="thaotac"></td>\
                                </tr>');
                                    checkcap2 = v.MA_CAP2;
                                }
                                var show = v.COMMENT_DV;
                                //if (v.COMMENT_DV.length > 15) {
                                //    show = v.COMMENT_DV.substring(0, 12) + "...";
                                //}
                                $('#bd').append('<tr chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '">\
                                <td>' + v.TEN_CHI_TIEU + '</td>\
                                 <td>' + v.DON_VI_TINH + '</td>\
                                <td><input type="text" disabled="disabled"  chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" class="e-input1 form-control" level="1" value="' + v.KeHoachDauNam + '" /></td>\
                                <td><input type="text"   chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" class="e-input3 form-control" disabled="disabled" level="3" title="Nội dung: ' + v.COMMENT_DV + '" value="' + v.ThucHien + '" /></td>\
                                <td><label title="Nội dung: ' + v.COMMENT_DV + '">' + show + '</label></td>\
                                <td><input type="text"   chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" class="e-input3 form-control" style="color:red;" level="61" value="' + v.ThucHien + '" /></td>\
                                <td><input type="button" class="btn btn-danger reject"  chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" style="width: 90px;" data-html="true" data-placement="bottom" level="12" title="Nội dung:"  value="Ý kiến" /></td>\
                                <td><button type="button" chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" style="" class="btn btn-primary approve">Duyệt & Gửi </button></td>\
                            </tr>');
                                lstvalue1.push(v.ThucHien);
                            }
                        });
                        $('#myModal').modal('show');
                    } else {
                        window.notice('Ban KHTC đã chốt kế hoạch ! ', window.notice_warring);
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {
                }
            });
        });
        $('.reject').live('click', function () {
            var chitieu = $(this).attr('chitieuid');
            var donvi = $(this).attr('donviid');
            $(this).popover({
                trigger: 'manual'
            });
            if ($(this).hasClass('pop')) {
                $(this)
                    .popover('hide')
                    .removeClass('pop');
            } else {
                var chitieu = $(this).attr('chitieuid');
                var donvi = $(this).attr('donviid');
                var contentnew = '<div class="row">\
            <div class="col-md-12">\
                <textarea class="form-control comment" chitieuid="' + chitieu + '" donviid="' + donvi + '" level="13" rows="6" ></textarea>\
            </div>\
            </div>\
            <br />\
            <div class="row">\
            <center>\
                <button type="button" chitieuid="' + chitieu + '"  donviid="' + donvi + '" class="btn ghinhan">Ghi nhận</button>\
                <button type="button" chitieuid="' + chitieu + '" donviid="' + donvi + '" class="btn huybo">Huỷ</button>\
            </center>\
            </div>';
                $(this)
                    .attr('data-content', contentnew)
                    .popover('show')
                    .addClass('pop');
            }

        });
        $('.reject').live('hide.bs.popover', function () {
            console.log(checkxem);
            if (checkxem == 0) {
                var chitieu = $(this).attr('chitieuid');
                var donvi = $(this).attr('donviid');
                var giatri = $('[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="61"]').val();
                var comment = "";
                $('[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="13"]').each(function () {
                    if ($(this).is(":visible")) {
                        comment = $(this).val();
                    }
                })
                $.each(lstcommentsend, function (i) {
                    if (lstcommentsend[i].CHI_TIEU_ID === chitieu && lstcommentsend[i].DON_VI_ID === donvi) {
                        lstcommentsend.splice(i, 1);
                        return false;
                    }
                });
                if (comment.trim() != "") {
                    var obj = {
                        'COMMENT_ID': 0,
                        'BAN_ID': 0,
                        'CHI_TIEU_ID': chitieu,
                        'COMMENT_BAN': comment,
                        'DON_VI_ID': donvi,
                        'GIA_TRI': giatri,
                        'COMMENT_DV': ' ',
                        'STATUS_ID': 2,
                        'IS_ACTIVE': 1,
                        'MOC': 2,
                        'NAM_KE_HOACH_ID': $('#Namlapbc').val()
                    }
                    lstcommentsend.push(obj);
                    $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="12"]').val("Warning");
                    $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="12"]').removeClass("btn-danger");
                    $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="12"]').addClass("btn-warning");
                } else {
                    $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="12"]').val("Ý kiến");
                    $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="12"]').removeClass("btn-warning");
                    $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="12"]').addClass("btn-danger");
                }
            }
        });
        $('.reject').live('shown.bs.popover', function () {
            var chitieu = $(this).attr('chitieuid');
            var donvi = $(this).attr('donviid');
            var comment = "";
            $.each(lstcommentsend, function (i, v) {
                if (v.CHI_TIEU_ID == chitieu && v.DON_VI_ID == donvi) {
                    comment = v.COMMENT_BAN;
                    return false;
                }
            });
            $('[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="13"]').text(comment);
        });
        $('.commnetban').live('click', function () {
            var chitieu = $(this).attr('chitieuid');
            var donvi = $(this).attr('donviid');
            $(this).popover({
                trigger: 'manual'
            });
            if ($(this).hasClass('pop')) {
                $(this)
                    .popover('hide')
                    .removeClass('pop');
            } else {
                var chitieu = $(this).attr('chitieuid');
                var donvi = $(this).attr('donviid');
                var contentnew = '<div class="row">\
                <div class="col-md-12">\
                    <textarea class="form-control comment" chitieuid="' + chitieu + '" donviid="' + donvi + '" level="10" rows="8" ></textarea>\
                </div>\
                </div>';
                $(this)
                    .attr('data-content', contentnew)
                    .popover('show')
                    .addClass('pop');
            }

        });
        $('.commnetban').live('shown.bs.popover', function () {
            var chitieu = $(this).attr('chitieuid');
            var donvi = $(this).attr('donviid');
            var comment = "";
            $.each(lstcomment, function (i, v) {
                if (v.CHI_TIEU_ID == chitieu && v.DON_VI_ID == donvi) {
                    comment = v.COMMENT_DV;
                    return false;
                }
            });
            $('[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="10"]').text(comment);
        })
        $('.ghinhan').live('click', function () {
            var chitieu = $(this).attr('chitieuid');
            var donvi = $(this).attr('donviid');
            $('[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="12"]').trigger('click');
        });
        $('.huybo').live('click', function () {
            var chitieu = $(this).attr('chitieuid');
            var donvi = $(this).attr('donviid');
            $('[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="13"]').val('');
            $('[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="12"]').trigger('click');
        });
        $('.approve').live('click', function () {
            var chitieu = $(this).attr('chitieuid');
            var donvi = $(this).attr('donviid');
            var nkh = $('#Namlapbc').val();
            var moc = 2;
            var kq = true;
            var lstkq = [];
            $.each(lstcommentsend, function (i) {
                if (lstcommentsend[i].CHI_TIEU_ID === chitieu && lstcommentsend[i].DON_VI_ID === donvi) {
                    if (lstcommentsend[i].COMMENT_BAN.trim() == "") {
                        kq = false;
                        return false;
                    } else {
                        lstkq.push(lstcommentsend[i]);
                    }

                }
            });
            if (kq == false) {
                $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="61"]').focus();
                window.notice('Ban vui lòng nhập lý do cho chỉ tiêu ! ', window.notice_warring);
            } else {
                if (lstcommentsend.length > 0) {
                    $.ajax({
                        url: initData.SaveCommnetByBan,
                        type: "POST",
                        headers: {
                            'VerificationToken': initData.VerificationToken
                        },
                        dataType: "json",
                        data: JSON.stringify({
                            'cmt': lstkq,
                        }),
                        contentType: 'application/json',
                        success: function (data) {
                            if (data == 0) {
                                window.notice('Bạn đã duyệt thất bại !', window.notice_error);
                            } else {
                                $('tr[chitieuid="' + chitieu + '"][donviid="' + donvi + '"]').remove();
                                $.each(lstcomment, function (i) {
                                    if (lstcomment[i].CHI_TIEU_ID === parseInt(chitieu) && lstcomment[i].DON_VI_ID === parseInt(donvi)) {
                                        lstcomment.splice(i, 1);
                                        return false;
                                    }
                                });
                                window.notice('Bạn đã duyệt thành công !', window.notice_success);
                            }
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                        },
                        complete: function () {
                        }
                    });
                } else {
                    window.notice('Ban vui lòng nhập lý do cho chỉ tiêu ! ', window.notice_warring);
                }
            }
        })
        $('#Namlapbc').on('change', function () {
            var data = $(this).val();
            if (data != -1) {
                loadtable(data, initData);
            }
            $.cookie('mkhidchung', $(this).val());
            $.session.set('nkhid', data);

        })
        $('#lstdstruong').live('change', function () {
            var dv = $(this).val();
            $('#bd').empty();
            var checkcap2 = "";
            $.each(lstcomment, function (i, v) {
                if (v.DON_VI_ID == dv) {
                    if (v.MA_CAP2 != checkcap2) {
                        $('#bd').append('<tr>\
                                    <td style="font-weight: bold;">' + v.TEN_CHI_TIEU_CAP2.toUpperCase() + '</td>\
                                     <td></td>\
                                    <td></td>\
                                    <td></td>\
                                    <td></td>\
                                    <td></td>\
                                    <td></td>\
                                    <td class="cmbankhtc" style="display:none;"></td>\
                                    <td class="thaotac"></td>\
                                    <td class="cmkhtcbutton"></td>\
                                </tr>');
                        checkcap2 = v.MA_CAP2;
                    }
                    var show = v.COMMENT_DV;
                    if (show == null) {
                        show = "";
                    }
                    var giatridonvi = v.ThucHien;
                    if (initData.DonviID == 0) {
                        var showcmban = v.COMMENT_BAN;
                        if (showcmban == null) {
                            showcmban = "";
                        }
                        giatridonvi = v.DieuChinh;
                    }
                    $('#bd').append('<tr chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '">\
                                <td>' + v.TEN_CHI_TIEU + '</td>\
                                 <td>' + v.DON_VI_TINH + '</td>\
                                <td><input type="text" disabled="disabled"  chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" class="e-input1 form-control" level="1" value="' + v.KeHoachDauNam + '" /></td>\
                                <td><input type="text"   chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" class="e-input3 form-control" disabled="disabled" level="3" value="' + giatridonvi + '" /></td>\
                                <td><label title="Nội dung: ' + v.COMMENT_DV + '">' + show + '</label></td>\
                                <td><input type="text"   chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" class="e-input3 form-control" level="61" style="color:red" value="' + v.ThucHien + '" /></td>\
                                <td class="cmbanbutton"><input type="button" class="btn btn-danger reject"  chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" style="width: 90px;" data-html="true" data-placement="bottom" level="12" title="Nội dung:"  value="Ý kiến" /></td>\
                                <td class="cmbanlabel"><label title="Nội dung: ' + v.COMMENT_BAN + '">' + showcmban + '</label></td>\
                                <td class="cmbankhtc" style="display:none;"><label chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" level="99" title=""></label></td>\
                                <td class="cmkhtcbutton" style="display:none;" chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '"><label chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" level="98"></label> <input type="button" class="btn btn-danger rejectkhtc"  chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" style="width: 90px;" data-html="true" data-placement="bottom" level="14" title="Nội dung:"  value="Ý kiến" /></td>\
                                <td class="thaotac"><button type="button" chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '"class="btn btn-primary approve">Duyệt & Gửi</button></td>\
                            </tr>');
                }
            });

            if ($('.dsdv').attr('colspan') == 5 || checkdhqg == 1) {
                $('.thaotac').hide();
            } else {
                $('.thaotac').show();
            }

            //checkxem ==1 : Xem thoong tin da duyet. ==0 ban dau
            if (checkxem == 1) {
                $('input[level="61"]').css('color', 'black');
                $('input[level="61"]').prop('disabled', true);
                $.each(lstcommentsend, function (i, v) {
                    $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="61"]').val(v.ThucHien);
                })
            } else {
                lstcommentsend = [];
                $('.dsdv').attr('colspan', 6);
            }
            if (checkdhqg == 1) {
                $('.cmbanlabel').show();
                $('.cmbanbutton').hide();
                $('.cmkhtcbutton').show();
                $('.dsdv').attr('colspan', 7);
                $('.cmbankhtc').show();
                $.ajax({
                    url: initData.GetallCommentNew,
                    type: "GET",
                    headers: {
                        'VerificationToken': initData.VerificationToken
                    },
                    dataType: "json",
                    data: {
                        'namkh': $('#Namlapbc').val(),
                        'moc': 2,
                        'statusid': 4
                    },
                    async: false,
                    success: function (data) {
                        $.each(data, function (i, v) {
                            if (v.IS_ACTIVE == 3) {
                                $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="61"]').css('background-color', '#76A3E6');
                                $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="61"]').css('color', 'white');
                                $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="99"]').text(v.COMMENT_BAN);
                            }
                            if (v.IS_ACTIVE == 0) {
                                $('label[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="98"]').text(v.COMMENT_DV);
                                $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="14"]').prop('disabled', true);
                                $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="99"]').text(v.COMMENT_BAN);
                            } else {
                                $('label[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="98"]').text(v.COMMENT_DV);
                                $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="14"]').prop('disabled', false);
                            }
                        });
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                    },
                    complete: function () {
                    }
                });
            } else {
                $('.cmbanlabel').hide();
                $('.cmbankhtc').hide();
                $('.cmbanbutton').show();
                $('.cmkhtcbutton').hide();
            }
            $.ajax({
                url: initData.GetallCommentNew,
                type: "GET",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                data: {
                    'namkh': $('#Namlapbc').val(),
                    'moc': 2,
                    'statusid': 2
                },
                async: false,
                success: function (data) {
                    $.each(data, function (i, v) {
                        if (v.IS_ACTIVE == 0) {
                            if (initData.DonviID == 0) {
                                $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="14"]').prop('disabled', true);
                            } else {
                                $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="61"]').css('background-color', '#B98A45');
                                $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="61"]').css('color', 'white');
                            }
                        }

                    });
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {
                }
            });
        })
        $('#ApproveAll').live('click', function () {

            var lst = [];
            $('input[level="3"]').each(function (i, v) {
                var donviid = $('#lstdstruong').val();
                var chitieu = $(this).attr('chitieuid');
                var giatri = $(this).val();
                if (giatri == "") {
                    giatri = " ";
                }
                var obj = {
                    'COMMENT_ID': 0,
                    'BAN_ID': 0,
                    'CHI_TIEU_ID': chitieu,
                    'COMMENT_BAN': '',
                    'DON_VI_ID': donviid,
                    'GIA_TRI': giatri,
                    'COMMENT_DV': " ",
                    'STATUS_ID': 3,
                    'IS_ACTIVE': 1,
                    'MOC': 2,
                    'NAM_KE_HOACH_ID': $('#Namlapbc').val()
                }
                lst.push(obj);
            });
            $.ajax({
                url: initData.UpdateCommentGiuanambyDV,
                type: "POST",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                data: JSON.stringify({
                    'cmt': lst,
                }),
                contentType: 'application/json',
                success: function (data) {
                    if (data == true) {
                        var nkh = $('#Namlapbc').val();
                        var moc = 2;
                        $.ajax({
                            url: initData.UpdateCommentApproveGiuanam,
                            type: "POST",
                            headers: {
                                'VerificationToken': initData.VerificationToken
                            },
                            dataType: "json",
                            data: {
                                'namkh': nkh,
                                'chitieu': '222',
                                'donvi': '222',
                                'moc': moc,
                                'flag': 2
                            },
                            success: function (data) {
                                if (data > 0) {
                                    window.notice('Bạn đã chấp nhận thành công', window.notice_success);
                                    loadtable($('#Namlapbc').val(), initData);
                                    $('#myModal').modal('hide');
                                } else {
                                    window.notice('Bạn đã chấp nhận thất bại', window.notice_error);
                                }

                            },
                            error: function (jqXHR, textStatus, errorThrown) {
                                window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                            },
                            complete: function () {
                            }
                        });
                    } else {
                        window.notice('Thất bại', window.notice_error);
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {
                }
            });



        });
        $('#Approve').live('click', function () {
            var lst = [];
            $('input[level="3"]').each(function (i, v) {
                var donviid = $('#lstdstruong').val();
                var chitieu = $(this).attr('chitieuid');
                var giatri = $(this).val();
                if (giatri == "") {
                    giatri = " ";
                }
                var obj = {
                    'COMMENT_ID': 0,
                    'BAN_ID': 0,
                    'CHI_TIEU_ID': chitieu,
                    'COMMENT_BAN': '',
                    'DON_VI_ID': donviid,
                    'GIA_TRI': giatri,
                    'COMMENT_DV': " ",
                    'STATUS_ID': 3,
                    'IS_ACTIVE': 1,
                    'MOC': 2,
                    'NAM_KE_HOACH_ID': $('#Namlapbc').val()
                }
                lst.push(obj);
            });
            $.ajax({
                url: initData.UpdateCommentGiuanambyDV,
                type: "POST",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                data: JSON.stringify({
                    'cmt': lst,
                }),
                contentType: 'application/json',
                success: function (data) {
                    if (data == true) {
                        var nkh = $('#Namlapbc').val();
                        var moc = 2;
                        $.ajax({
                            url: initData.UpdateCommentApproveGiuanam,
                            type: "POST",
                            headers: {
                                'VerificationToken': initData.VerificationToken
                            },
                            dataType: "json",
                            data: {
                                'namkh': nkh,
                                'chitieu': '22222',
                                'donvi': $('#lstdstruong').val(),
                                'moc': moc,
                                'flag': 1
                            },
                            success: function (data) {
                                if (data > 0) {
                                    window.notice('Bạn đã chấp nhận thành công', window.notice_success);
                                    loadtable($('#Namlapbc').val(), initData);
                                    $('#myModal').modal('hide');
                                } else {
                                    window.notice('Bạn đã chấp nhận thất bại', window.notice_error);
                                }

                            },
                            error: function (jqXHR, textStatus, errorThrown) {
                                window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                            },
                            complete: function () {
                            }
                        });
                    } else {
                        window.notice('Thất bại', window.notice_error);
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {
                }
            });
        });
        $("#Duyet").live('click', function () {
            $("#ApproveAll").hide();
            $('#Huy').show();
            checkxem = 1;
            lstcommentsend = [];

            $("#Approve").hide();
            $("#Tit").text("Xem đề xuất điều chỉnh giữa năm đã phê duyệt");
            $.ajax({
                url: initData.GetallCommentByDv,
                type: "GET",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                data: {
                    'namkh': $('#Namlapbc').val(),
                    'moc': 2,
                    'flag': 3
                },
                async: false,
                success: function (data) {
                    $('#lstdstruong').empty();
                    if (data.length > 0 && data != "err") {
                        lstcomment = [];
                        var mtpdv = 0;
                        $.each(data, function (i, v) {
                            if (v.DON_VI_ID != mtpdv) {
                                $('#lstdstruong').append('<option value="' + v.DON_VI_ID + '">' + v.TenDV + '</option>');
                                mtpdv = v.DON_VI_ID;
                            }
                            lstcomment.push(v);
                        });
                        $('#bd').empty();
                        var dv = $('#lstdstruong').val();
                        var checkcap2 = "";

                        $.each(lstcomment, function (i, v) {
                            if (v.DON_VI_ID == dv) {
                                var dc = "";
                                if (v.DieuChinh != null) {
                                    dc = v.DieuChinh;
                                }
                                if (v.MA_CAP2 != checkcap2) {
                                    $('#bd').append('<tr>\
                                    <td style="font-weight: bold;">' + v.TEN_CHI_TIEU_CAP2.toUpperCase() + '</td>\
                                    <td></td>\
                                    <td></td>\
                                    <td></td>\
                                    <td></td>\
                                    <td></td>\
                                    <td></td>\
                                    <td class="cmbankhtc" style="display:none;"></td>\
                                    <td class="thaotac"></td>\
                                    <td class="cmkhtcbutton"></td>\
                                </tr>');
                                    checkcap2 = v.MA_CAP2;
                                }
                                var show = v.COMMENT_DV;
                                //if (v.COMMENT_DV.length > 15) {
                                //    show = v.COMMENT_DV.substring(0, 12) + "...";
                                //}
                                var giatridonvi = v.ThucHien;
                                if (initData.DonviID == 0) {
                                    var showcmban = v.COMMENT_BAN;
                                    //if (v.COMMENT_BAN.length > 15) {
                                    //    showcmban = v.COMMENT_BAN.substring(0, 12) + "...";
                                    //}
                                    giatridonvi = v.DieuChinh
                                }
                                $('#bd').append('<tr chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '">\
                                <td>' + v.TEN_CHI_TIEU + '</td>\
                                 <td>' + v.DON_VI_TINH + '</td>\
                                <td><input type="text" disabled="disabled"  chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" class="e-input1 form-control" level="1" value="' + v.KeHoachDauNam + '" /></td>\
                                <td><input type="text"   chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" disabled="disabled" class="e-input3 form-control" level="3" value="' + giatridonvi + '" /></td>\
                                <td><label title="Nội dung: ' + v.COMMENT_DV + '">' + show + '</label></td>\
                                <td><input type="text"   chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" disabled="disabled" class="e-input1 form-control" level="61" value="" /></td>\
                                <td class="cmbanbutton"><input type="button" class="btn btn-danger reject"  chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" style="width: 90px;" data-html="true" data-placement="bottom" level="12" title="Nội dung:"  value="Ý kiến" /></td>\
                                <td class="cmbanlabel"><label title="Nội dung: ' + v.COMMENT_BAN + '">' + showcmban + '</label></td>\
                                <td class="cmbankhtc" style="display:none;"><label chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" level="99"" title=""></label></td>\
                                <td class="cmkhtcbutton" style="display:none;" chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '"><label chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" level="98"></label><input type="button" class="btn btn-danger rejectkhtc"  chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" style="width: 90px;" data-html="true" data-placement="bottom" level="14" title="Nội dung:"  value="Ý kiến" /></td>\
                                <td class="thaotac"><button type="button" chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '"  class="btn btn-primary approve"> Duyệt & Gửi</button></td>\
                            </tr>');
                            }
                        });

                        $.ajax({
                            url: initData.GetallCommentByDv,
                            type: "GET",
                            headers: {
                                'VerificationToken': initData.VerificationToken
                            },
                            dataType: "json",
                            data: {
                                'namkh': $('#Namlapbc').val(),
                                'moc': 2,
                                'flag': 5
                            },
                            async: false,
                            success: function (data) {
                                $.each(data, function (i, v) {
                                    lstcommentsend.push(v);
                                    $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="61"]').val(v.ThucHien);
                                });
                                $.ajax({
                                    url: initData.GetallCommentNew,
                                    type: "GET",
                                    headers: {
                                        'VerificationToken': initData.VerificationToken
                                    },
                                    dataType: "json",
                                    data: {
                                        'namkh': $('#Namlapbc').val(),
                                        'moc': 2,
                                        'statusid': 2
                                    },
                                    async: false,
                                    success: function (data) {
                                        $.each(data, function (i, v) {

                                            if (v.IS_ACTIVE == 0) {
                                                if (initData.DonviID == 0) {
                                                    $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="14"]').prop('disabled', true);
                                                } else {
                                                    $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="61"]').css('background-color', '#B98A45');
                                                    $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="61"]').css('color', 'white');
                                                }
                                            }

                                        });
                                    },
                                    error: function (jqXHR, textStatus, errorThrown) {
                                        window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                                    },
                                    complete: function () {
                                    }
                                });
                            },
                            error: function (jqXHR, textStatus, errorThrown) {
                                window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                            },
                            complete: function () {
                            }
                        });

                        $('.dsdv').attr('colspan', 5);
                        $('.thaotac').hide();
                        $('#myModal').modal('show');
                    } else {
                        window.notice('Không có chỉ tiêu nào được duyệt ', window.notice_warring);
                    }

                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {
                }
            });
            if (initData.DonviID == 0) {
                checkdhqg = 1;
                $('#Full').show();
                $('#FeedBack').show();
                $('#Huy').hide();
                $.ajax({
                    url: initData.GetNamkehoachbyID,
                    type: "GET",
                    headers: {
                        'VerificationToken': initData.VerificationToken
                    },
                    dataType: "json",
                    data: {
                        'id': $('#Namlapbc').val(),
                    },
                    async: false,
                    success: function (data) {
                        $.ajax({
                            url: initData.GetallCommentNew,
                            type: "GET",
                            headers: {
                                'VerificationToken': initData.VerificationToken
                            },
                            dataType: "json",
                            data: {
                                'namkh': $('#Namlapbc').val(),
                                'moc': 2,
                                'statusid': 4
                            },
                            async: false,
                            success: function (data) {
                                $.each(data, function (i, v) {
                                    if (v.IS_ACTIVE == 3) {
                                        $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="61"]').css('background-color', '#76A3E6');
                                        $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="61"]').css('color', 'white');
                                        $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="99"]').text(v.COMMENT_BAN);
                                    }
                                    if (v.IS_ACTIVE == 0) {
                                        $('label[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="98"]').text(v.COMMENT_DV);
                                        $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="14"]').prop('disabled', true);
                                        $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="99"]').text(v.COMMENT_BAN);
                                    } else {
                                        $('label[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="98"]').text(v.COMMENT_DV);
                                        $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="14"]').prop('disabled', false);
                                    }
                                });
                            },
                            error: function (jqXHR, textStatus, errorThrown) {
                                window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                            },
                            complete: function () {
                            }
                        });
                        if (data.STATUS_ID < 70) {
                            $('#Full').show();
                            $('#FeedBack').show();
                        } else {
                            $('#Full').hide();
                            $('#FeedBack').hide();
                        }
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                    },
                    complete: function () {
                    }
                });
                $('.cmbanlabel').show();
                $('.cmbanbutton').hide();
                $('.cmkhtcbutton').show();
                $('.cmbankhtc').show();
                $('.dsdv').attr('colspan', 7);


            } else {
                $('.cmbanlabel').hide();
                $('.cmbanbutton').show();
                $('.cmbankhtc').hide();
                $('#Full').hide();
                $('#Huy').show();
                $('.cmkhtcbutton').hide();
                $('.dsdv').attr('colspan', 5);
            }
        })
        $('#Huy').live('click', function () {
            $.ajax({
                url: initData.GetNamkehoachbyID,
                type: "GET",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                data: {
                    'id': $('#Namlapbc').val(),
                },
                async: false,
                success: function (data) {
                    if (data.STATUS_ID < 70) {
                        window.notice('Bạn đã hủy thành công ! ', window.notice_success);
                        $('#Huy').hide();
                        $('.dsdv').attr('colspan', 6);
                        $('.thaotac').show();
                        $('input[level="61"]').prop('disabled', false);
                        $('input[level="61"]').css('background', '#F7CECE');
                        $('input[level="61"]').css('color', '#f00');
                        checkxem = 0;
                        lstcommentsend = [];
                    } else {
                        $('#Huy').show();
                        $('.dsdv').attr('colspan', 5);
                        $('.thaotac').hide();
                        $('input[level="61"]').prop('disabled', true);
                        window.notice('Ban KHTC đã chốt kế hoạch ! ', window.notice_warring);
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {
                }
            });
        })
        $('#Full').live('click', function () {
            var namkh = $('#Namlapbc').val();
            $.ajax({
                url: initData.Getbanchuaguicomment,
                type: "GET",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                data: {
                    'namkh': namkh,
                    'moc': 2
                },
                success: function (data) {
                    if (data.length > 0) {
                        $('#dsban').empty();
                        $('#myModal').modal('hide');
                        $.each(data, function (i, v) {
                            $('#dsban').append('<tr class="webgrid-row-style"><td>\
                                          <div id="spnFullName" class="display-mode">\
                                           <span class="hidden-780 ">' + (i + 1) + '</span>\
                                         </div>\
                                </td>\
                                    <td>\
                                          <div id="spnFullName" class="display-mode">\
                                           <span class="hidden-780 ">' + v.DON_VI + '</span>\
                                         </div>\
                                </td></tr>\
                    ');
                        });
                        $('#myModalDSban').modal('show');
                    } else {
                        var r = confirm("Bạn có chắc muốn duyệt kế hoạch giữa năm không ?");
                        if (r == true) {

                            $.ajax({
                                url: initData.ChotDieuChinhGiuaNam,
                                type: "POST",
                                headers: {
                                    'VerificationToken': initData.VerificationToken
                                },
                                dataType: "json",
                                data: {
                                    'namkh': namkh,
                                },
                                success: function (data) {
                                    if (data != 0) {
                                        window.notice('Bạn đã duyệt thành công ! ', window.notice_success);
                                        $('#myModal').modal('hide');
                                    }

                                },
                                error: function (jqXHR, textStatus, errorThrown) {
                                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                                },
                                complete: function () {

                                }
                            });
                        }
                    }

                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {

                }
            });

        })
        $('#nhomdonvi').on('change', function () {
            loaddatatotable($('#nhomchitieu').val(), $('#nhomdonvi').val(), 61, initData, $('#Namlapbc').val());
        });
        $('#nhomchitieu').on('change', function () {
            loaddatatotable($('#nhomchitieu').val(), $('#nhomdonvi').val(), 61, initData, $('#Namlapbc').val());
        });
        $('input[level="61"]').live('change', function () {
            var chitieu = $(this).attr('chitieuid');
            var donvi = $(this).attr('donviid');
            var data = $(this).val();
            var kq = false;
            $.each(lstcommentsend, function (i) {
                if (lstcommentsend[i].CHI_TIEU_ID === chitieu && lstcommentsend[i].DON_VI_ID == donvi) {
                    lstcommentsend[i].GIA_TRI = data;
                    kq = true;
                    return false;
                }
            });
            if (kq == false) {
                var obj = {
                    'COMMENT_ID': 0,
                    'BAN_ID': 0,
                    'CHI_TIEU_ID': chitieu,
                    'COMMENT_BAN': ' ',
                    'DON_VI_ID': donvi,
                    'GIA_TRI': $(this).val(),
                    'COMMENT_DV': ' ',
                    'STATUS_ID': 2,
                    'IS_ACTIVE': 1,
                    'MOC': 2,
                    'NAM_KE_HOACH_ID': $('#Namlapbc').val()
                }
                lstcommentsend.push(obj);
                $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="12"]').val("Warning");
                $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="12"]').removeClass("btn-danger");
                $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="12"]').addClass("btn-warning");
            }

        })
        $('#Xemfull').live('click', function () {
            $.ajax({
                url: initData.GetNamkehoachbyID,
                type: "GET",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                data: {
                    'id': $('#Namlapbc').val(),
                },
                async: false,
                success: function (data) {
                    if (data.STATUS_ID >= 70) {
                        stus = 61;
                        $('#myModalLabel').text('Xem điều chỉnh kế hoạch định hướng giữa năm');
                        var namkehoachid = $('#Namlapbc').val();
                        $.session.set('nkhid', namkehoachid);
                        $.ajax({
                            url: initData.GetallChitieubynamchitieu,
                            type: "GET",
                            headers: {
                                'VerificationToken': initData.VerificationToken
                            },
                            dataType: "json",
                            async: false,
                            data: {
                                'namchitieuid': namkehoachid,
                                'madonvi': initData.DonviID
                            },
                            success: function (data) {
                                $('#nhomchitieu').empty();
                                $.each(data, function (i, v) {
                                    if (v.CAP == 2) {
                                        $('#nhomchitieu').append('<option value="' + v.MA_CAP2 + '">' + v.TEN_CHI_TIEU_CAP2 + '</option>')
                                    }
                                })
                            },
                            error: function (jqXHR, textStatus, errorThrown) {
                                window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                            },
                            complete: function () {

                            }
                        });
                        $.ajax({
                            url: initData.Getallnhomdonvi,
                            type: "GET",
                            headers: {
                                'VerificationToken': initData.VerificationToken
                            },
                            dataType: "json",
                            async: false,
                            data: {
                                'namchitieuid': namkehoachid
                            },
                            success: function (data) {
                                $('#nhomdonvi').empty();
                                $.each(data, function (i, v) {
                                    $('#nhomdonvi').append('<option value="' + v.NhomDonViId + '">' + v.TenNhomDonVi + '</option>')
                                })
                            },
                            error: function (jqXHR, textStatus, errorThrown) {
                                window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                            },
                            complete: function () {

                            }
                        });
                        loaddatatotable($('#nhomchitieu').val(), $('#nhomdonvi').val(), stus, initData, namkehoachid);
                        $('#Update').hide();
                        $('#Chot').hide();
                        $('#myModalFull').modal('show');
                    } else {
                        window.notice('Ban KHTC chưa chốt kế hoạch ! ', window.notice_warring);
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {
                }
            });

        });

        $('.rejectkhtc').live('click', function () {
            var chitieu = $(this).attr('chitieuid');
            var donvi = $(this).attr('donviid');
            $(this).popover({
                trigger: 'manual'

            });
            if ($(this).hasClass('pop')) {
                $(this)
                    .popover('hide')
                    .removeClass('pop');
            } else {
                var chitieu = $(this).attr('chitieuid');
                var donvi = $(this).attr('donviid');
                var contentnew = '<div class="row">\
            <div class="col-md-12">\
                <textarea class="form-control comment" chitieuid="' + chitieu + '" donviid="' + donvi + '" level="15" rows="6" ></textarea>\
            </div>\
            </div>\
            <br />\
            <div class="row">\
            <center>\
                <button type="button" chitieuid="' + chitieu + '"  donviid="' + donvi + '" class="btn ghinhankhtc">Ghi nhận</button>\
                <button type="button" chitieuid="' + chitieu + '" donviid="' + donvi + '" class="btn huybokhtc">Huỷ</button>\
            </center>\
            </div>';
                $(this)
                    .attr('data-content', contentnew)
                    .popover('show')
                    .addClass('pop');
            }
        });
        $('.rejectkhtc').live('hide.bs.popover', function () {
            var chitieu = $(this).attr('chitieuid');
            var donvi = $(this).attr('donviid');
            var comment = $('[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="15"]').val();
            $.each(lstcommentkhtc, function (i) {
                if (lstcommentkhtc[i].CHI_TIEU_ID === chitieu && lstcommentkhtc[i].DON_VI_ID === donvi) {
                    lstcommentkhtc.splice(i, 1);
                    return false;
                }
            });
            if (comment.trim() != "") {
                var obj = {
                    'COMMENT_ID': 0,
                    'BAN_ID': 0,
                    'CHI_TIEU_ID': chitieu,
                    'COMMENT_BAN': ' ',
                    'DON_VI_ID': donvi,
                    'GIA_TRI': 0,
                    'COMMENT_DV': comment,
                    'STATUS_ID': 2,
                    'IS_ACTIVE': 0,
                    'MOC': 2,
                    'NAM_KE_HOACH_ID': $('#Namlapbc').val()
                }
                lstcommentkhtc.push(obj);
                $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="14"]').val("Warning");
                $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="14"]').removeClass("btn-danger");
                $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="14"]').addClass("btn-warning");
            } else {
                $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="14"]').val("Ý kiến");
                $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="14"]').removeClass("btn-warning");
                $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="14"]').addClass("btn-danger");
            }
        });
        $('.rejectkhtc').live('shown.bs.popover', function () {
            var chitieu = $(this).attr('chitieuid');
            var donvi = $(this).attr('donviid');
            var comment = "";
            $.each(lstcommentkhtc, function (i, v) {
                if (v.CHI_TIEU_ID == chitieu && v.DON_VI_ID == donvi) {
                    comment = v.COMMENT_DV;
                    return false;
                }
            });
            $('[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="15"]').text(comment);
        });
        $('.ghinhankhtc').live('click', function () {
            var chitieu = $(this).attr('chitieuid');
            var donvi = $(this).attr('donviid');
            $('[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="14"]').trigger('click');
        });
        $('.huybokhtc').live('click', function () {
            var chitieu = $(this).attr('chitieuid');
            var donvi = $(this).attr('donviid');
            $('[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="15"]').val('');
            $('[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="14"]').trigger('click');
        });
        $('#FeedBack').live('click', function () {
            if (lstcommentkhtc.length > 0) {
                $.ajax({
                    url: initData.UpdateCommentbyKHTC,
                    type: "POST",
                    headers: {
                        'VerificationToken': initData.VerificationToken
                    },
                    dataType: "json",
                    data: JSON.stringify({
                        'cmt': lstcommentkhtc,
                    }),
                    contentType: 'application/json',
                    success: function (data) {
                        if (data > 0) {
                            lstcommentkhtc = [];
                            window.notice('Bạn đã gửi tới ban thành công !', window.notice_success);
                        } else {
                            window.notice('Bạn đã gửi tới ban thất bại !', window.notice_error);
                        }
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                    },
                    complete: function () {
                    }
                });
            } else {
                window.notice('Không có ý kiến nào được thiết lập ! ', window.notice_warring);
            }
        })
        $('#Update').on('click', function () {
            checkxem = 0;
            $.ajax({
                url: initData.GetNamkehoachbyID,
                type: "GET",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                data: {
                    'id': $('#Namlapbc').val(),
                },
                async: false,
                success: function (data) {
                    lstcommentsend = [];
                    lstcommentstatus2 = [];
                    lstcommentstatus3 = [];
                    lstcommentstatus4 = [];
                    if (data.STATUS_ID < 70) {
                        $.ajax({
                            url: initData.GetallCommentByDv,
                            type: "GET",
                            headers: {
                                'VerificationToken': initData.VerificationToken
                            },
                            dataType: "json",
                            data: {
                                'namkh': $('#Namlapbc').val(),
                                'moc': 2,
                                'flag': 7
                            },
                            async: false,
                            success: function (data) {
                                lstcomment = [];
                                var mtpdv = 0;
                                $('#lstdstruongnew').empty();
                                $.each(data, function (i, v) {
                                    if (v.DON_VI_ID != mtpdv) {
                                        $('#lstdstruongnew').append('<option value="' + v.DON_VI_ID + '">' + v.TenDV + '</option>');
                                        mtpdv = v.DON_VI_ID;
                                    }

                                    lstcomment.push(v);
                                });
                            },
                            error: function (jqXHR, textStatus, errorThrown) {
                                window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                            },
                            complete: function () {
                            }
                        });
                        $('#bdnew').empty();
                        var dv = $('#lstdstruongnew').val();
                        lstvalue2 = [];
                        var checkcap2 = "";
                        $.each(lstcomment, function (i, v) {
                            if (v.DON_VI_ID == dv) {
                                var dc = "";
                                if (v.DieuChinh != null) {
                                    dc = v.DieuChinh;
                                }
                                if (v.MA_CAP2 != checkcap2) {
                                    $('#bdnew').append('<tr>\
                                    <td style="font-weight: bold;">' + v.TEN_CHI_TIEU_CAP2.toUpperCase() + '</td>\
                                    <td></td>\
                                    <td></td>\
                                    <td></td>\
                                    <td></td>\
                                    <td></td>\
                                    <td></td>\
                                    <td></td>\
                                    <td></td>\
                                    <td class="thaotac"></td>\
                                </tr>');
                                    checkcap2 = v.MA_CAP2;
                                }
                                var show = v.COMMENT_DV;
                                if (show == null) {
                                    show = "";
                                }
                                var showban = v.COMMENT_BAN;
                                if (showban == null) {
                                    showban = "";
                                }
                                $('#bdnew').append('<tr chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '">\
                                <td>' + v.TEN_CHI_TIEU + '</td>\
                                 <td>' + v.DON_VI_TINH + '</td>\
                                <td><input type="text" disabled="disabled"  chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" class="e-input1 form-control" level="1" value="' + v.KeHoachDauNam + '" /></td>\
                                <td><input type="text"   chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" class="e-input3 form-control" disabled="disabled" level="3" value="' + v.ThucHien + '" /></td>\
                                <td><label chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" level="97"></label></td>\
                                <td><input type="text"   chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" class="e-input3 form-control" level="61" value="' + v.DieuChinh + '" /></td>\
                                <td><label chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" level="96">' + showban + '</label></td>\
                                <td><label>' + show + '</label></td>\
                                <td><input type="button" class="btn btn-danger reject"  chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" style="width: 90px;" data-html="true" disabled="disabled" data-placement="bottom" level="12" title="Nội dung:"  value="Ý kiến" /></td>\
                                <td><button type="button" chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" style="" disabled="disabled" class="btn btn-primary send">Gửi ý kiến</button></td>\
                            </tr>');
                                lstvalue2.push(v.ThucHien);
                            }
                        });
                        $.ajax({
                            url: initData.GetallCommentNew,
                            type: "GET",
                            headers: {
                                'VerificationToken': initData.VerificationToken
                            },
                            dataType: "json",
                            data: {
                                'namkh': $('#Namlapbc').val(),
                                'moc': 2,
                                'statusid': 3
                            },
                            async: false,
                            success: function (data) {
                                $.each(data, function (i, v) {
                                    $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="97"]').text(v.COMMENT_DV);
                                    lstcommentstatus2.push(v);
                                });
                            },
                            error: function (jqXHR, textStatus, errorThrown) {
                                window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                            },
                            complete: function () {
                            }
                        });
                        $.ajax({
                            url: initData.GetallCommentNew,
                            type: "GET",
                            headers: {
                                'VerificationToken': initData.VerificationToken
                            },
                            dataType: "json",
                            data: {
                                'namkh': $('#Namlapbc').val(),
                                'moc': 2,
                                'statusid': 4
                            },
                            async: false,
                            success: function (data) {
                                $.each(data, function (i, v) {
                                    if (v.IS_ACTIVE == 0) {
                                        $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="12"]').prop('disabled', false);
                                        $('button[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"]').prop('disabled', false);
                                        if (v.COMMENT_BAN != null && v.COMMENT_BAN.trim() != "") {
                                            $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="96"]').text(v.COMMENT_BAN);
                                        }

                                    }
                                    if (v.IS_ACTIVE == 3) {
                                        $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="96"]').text(v.COMMENT_BAN);
                                    }
                                    lstcommentstatus4.push(v);
                                });
                            },
                            error: function (jqXHR, textStatus, errorThrown) {
                                window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                            },
                            complete: function () {
                            }
                        });


                        if (lstcomment.length > 0) {
                            $('#myModalNew').modal('show');
                        } else {
                            window.notice('Không có chỉ tiêu phản hồi ! ', window.notice_warring);
                        }
                    } else {
                        window.notice('Ban KHTC đã chốt kế hoạch ! ', window.notice_warring);
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {
                }
            });
        });
        $('#lstdstruongnew').live('change', function () {
            $('#bdnew').empty();
            var dv = $('#lstdstruongnew').val();
            lstvalue2 = [];
            lstcommentsend = [];
            var checkcap2 = "";
            $.each(lstcomment, function (i, v) {
                if (v.DON_VI_ID == dv) {
                    var dc = "";
                    if (v.DieuChinh != null) {
                        dc = v.DieuChinh;
                    }
                    if (v.MA_CAP2 != checkcap2) {
                        $('#bdnew').append('<tr>\
                                    <td style="font-weight: bold;">' + v.TEN_CHI_TIEU_CAP2.toUpperCase() + '</td>\
                                     <td></td>\
                                    <td></td>\
                                    <td></td>\
                                    <td></td>\
                                    <td></td>\
                                    <td></td>\
                                    <td></td>\
                                    <td></td>\
                                    <td class="thaotac"></td>\
                                </tr>');
                        checkcap2 = v.MA_CAP2;
                    }
                    var show = v.COMMENT_DV;
                    if (show == null) {
                        show = "";
                    }
                    var showban = v.COMMENT_BAN;
                    if (showban == null) {
                        showban = "";
                    }
                    $('#bdnew').append('<tr chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '">\
                                <td>' + v.TEN_CHI_TIEU + '</td>\
                                 <td>' + v.DON_VI_TINH + '</td>\
                                <td><input type="text" disabled="disabled"  chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" class="e-input1 form-control" level="1" value="' + v.KeHoachDauNam + '" /></td>\
                                <td><input type="text"   chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" class="e-input3 form-control" disabled="disabled" level="3" title="Nội dung: ' + v.COMMENT_DV + '" value="' + v.ThucHien + '" /></td>\
                                <td><label chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" level="97"></label></td>\
                                <td><input type="text"   chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" class="e-input3 form-control" level="61" value="' + v.DieuChinh + '" /></td>\
                                <td><label chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" level="96">' + showban + '</label></td>\
                                <td><label title="Nội dung: ' + v.COMMENT_DV + '">' + show + '</label></td>\
                                <td><input type="button" class="btn btn-danger reject"  chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" style="width: 90px;" data-html="true" data-placement="bottom" level="12" disabled="disabled" title="Nội dung:"  value="Ý kiến" /></td>\
                                <td><button type="button" chitieuid="' + v.CHI_TIEU_ID + '" donviid="' + v.DON_VI_ID + '" style="width: 90px;" disabled="disabled" class="btn btn-primary send">Gửi</button></td>\
                            </tr>');
                    lstvalue2.push(v.ThucHien);
                }
            });
            $.each(lstcommentstatus2, function (i, v) {
                $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="97"]').text(v.COMMENT_DV);
            })
            $.ajax({
                url: initData.GetallCommentNew,
                type: "GET",
                headers: {
                    'VerificationToken': initData.VerificationToken
                },
                dataType: "json",
                data: {
                    'namkh': $('#Namlapbc').val(),
                    'moc': 2,
                    'statusid': 4
                },
                async: false,
                success: function (data) {
                    $.each(data, function (i, v) {
                        if (v.IS_ACTIVE == 0) {
                            $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="12"]').prop('disabled', false);
                            $('button[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"]').prop('disabled', false);
                        }
                        if (v.IS_ACTIVE == 3) {
                            $('[chitieuid="' + v.CHI_TIEU_ID + '"][donviid="' + v.DON_VI_ID + '"][level="96"]').text(v.COMMENT_BAN);
                        }
                    });
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                },
                complete: function () {
                }
            });

        });
        $('.send').live('click', function () {
            var chitieu = $(this).attr('chitieuid');
            var donvi = $(this).attr('donviid');
            var nkh = $('#Namlapbc').val();
            var moc = 2;
            var kq = true;
            var lstkq = [];
            $.each(lstcommentsend, function (i) {
                if (lstcommentsend[i].CHI_TIEU_ID === chitieu && lstcommentsend[i].DON_VI_ID === donvi) {
                    if (lstcommentsend[i].COMMENT_BAN.trim() == "") {
                        kq = false;
                        return false;
                    } else {
                        lstkq.push(lstcommentsend[i]);
                    }

                }
            });
            if (kq == false) {
                $('input[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="61"]').focus();
                window.notice('Ban vui lòng nhập lý do cho chỉ tiêu ! ', window.notice_warring);
            } else {
                if (lstcommentsend.length > 0) {
                    $.ajax({
                        url: initData.UpdateCommentbyKHTC_BAN,
                        type: "POST",
                        headers: {
                            'VerificationToken': initData.VerificationToken
                        },
                        dataType: "json",
                        data: JSON.stringify({
                            'cmt': lstkq,
                        }),
                        contentType: 'application/json',
                        success: function (data) {
                            if (data == 0) {
                                window.notice('Bạn đã duyệt thất bại !', window.notice_error);
                            } else {
                                //$('tr[chitieuid="' + chitieu + '"][donviid="' + donvi + '"]').remove();
                                window.notice('Bạn đã duyệt thành công !', window.notice_success);
                                $('[chitieuid="' + chitieu + '"][donviid="' + donvi + '"][level="12"]').prop('disabled', true);
                                $('button[chitieuid="' + chitieu + '"][donviid="' + donvi + '"]').prop('disabled', true);

                                //$.each(lstcomment, function (i) {
                                //    if (lstcomment[i].CHI_TIEU_ID === parseInt(chitieu) && lstcomment[i].DON_VI_ID === parseInt(donvi)) {
                                //        lstcomment.splice(i, 1);
                                //        return false;
                                //    }
                                //});
                                //if (lstcomment.length == 0) {
                                //    $('#myModalNew').modal('hide');
                                //}
                            }
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                        },
                        complete: function () {
                        }
                    });
                } else {
                    window.notice('Ban vui lòng nhập lý do cho chỉ tiêu ! ', window.notice_warring);
                }
            }
        })
    }
    return {
        init: function (initData) {
            //load data
            initLoadData(initData);
            initButtonClick(initData);
        }
    }
}();